package com.huey.tl.threads;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * ┃　　　┃ 神兽保佑
 * ┃　　　┃ 代码无BUG！
 * ┃　　　┗━━━┓
 * ┃　　　　　　　┣┓
 * ┃　　　　　　　┏┛
 * ┗┓┓┏━┳┓┏┛
 * ┃┫┫　┃┫┫
 * ┗┻┛　┗┻┛
 *
 * @author huey
 * @Description : SchedulePoolTest
 * @Date Created in 2018/9/21 9:17
 */
public class SchedulePoolTest {


    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
//        sumbitTest(scheduledExecutorService);
//        scheduleTest(scheduledExecutorService);
        scheduleAtFixedRateTest(scheduledExecutorService);
//        scheduleWithFixedDelayTest(scheduledExecutorService);
//        scheduledExecutorService.shutdown();
    }

    /**
     * @author huey
     * @Description : 同普通线程池
     * @Date Created in 2018/9/21 9:37
     */
    public static void sumbitTest(ScheduledExecutorService scheduledExecutorService) {
        scheduledExecutorService.submit(() -> System.out.println("huey schedulePool"));
    }

    /**
     * @author huey
     * @Description : 延迟3秒执行
     * @Date Created in 2018/9/21 9:37
     */
    public static void scheduleTest(ScheduledExecutorService scheduledExecutorService) {
        scheduledExecutorService.schedule(() -> {
            System.out.println("scheduleTest");
        }, 3, TimeUnit.SECONDS);
    }

    /**
     * @author huey
     * @Description : 定时周期执行
     * @Date Created in 2018/9/21 9:40
     */
    public static void scheduleAtFixedRateTest(ScheduledExecutorService scheduledExecutorService) {
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println("scheduleAtFixedRateTest");
        }, 1,2, TimeUnit.SECONDS);
    }

    public static void scheduleWithFixedDelayTest(ScheduledExecutorService scheduledExecutorService) {
        scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                System.out.println("scheduleWithFixedDelayTest");
                //  throw new RuntimeException(); //有异常了就不会在执行了
            }
        }, 0, 5, TimeUnit.SECONDS);
    }
}
